package easy.最后一个单词的长度;

public class Solution {
    public int lengthOfLastWord(String s) {
        int index = s.length() - 1;
        while (s.charAt(index) == ' ') {
            index--;
        }
        int wordLength = 0;
        while (index >= 0 && s.charAt(index) != ' ') {
            wordLength++;
            index--;
        }
        return wordLength;
    }

    public static void main(String[] args) {
        String s = "Hello World";
        String s2 = "   fly me to the moon  ";
        String s3 = "a";
        Solution solution = new Solution();
        System.out.println(solution.lengthOfLastWord(s));
        System.out.println(solution.lengthOfLastWord(s2));
        System.out.println(solution.lengthOfLastWord(s3));
    }
}
